var QQMapWX = require("../lib/js/qqmap-wx-jssdk.min.js");

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


//分割一段字符串中的参数
function urlParameterSplit(separator, data, back) {
  const urlParameter = data.split(separator);
  const urlParameterList = new Array();
  for (var i = 0; i < urlParameter.length; i++) {
    urlParameterList.push(urlParameter[i].substring(urlParameter[i].lastIndexOf("=") + 1));
  }
  console.log(urlParameterList);
  back(urlParameterList);
}

function parameterIsNull(data, isNull) {
  if (data != null && data != "undefined" && data != undefined && data != "") {
    isNull(false);
  } else {
    isNull(true);
  }
}
/**
 * 获取用户地址
 */
function getLocation(callback) {
  var demo = new QQMapWX({
    key: "WSMBZ-JPKWP-HR6DQ-VBXX4-UPOP5-XLFCF"
  })
  //地图地址
  wx.getLocation({
    type: "gcj02",
    success(res) {
      const latitude = res.latitude;
      const longitude = res.longitude;
      const speed = res.speed;
      const accuracy = res.accuracy;
      demo.reverseGeocoder({
        location: {
          latitude: res.latitude,
          longitude: res.longitude
        },
        success: function(res) {
          callback(res.result);
        },
        fail: function(res) {
          console.log(res);
        },
      })
    },
    fail:function(res){
      wx.showModal({
        title: '提示',
        content: '请打开您手机的位置信息',
      })
    }
  })
}


module.exports = {
  formatTime: formatTime,
  urlParameterSplit: urlParameterSplit,
  parameterIsNull: parameterIsNull,
  getLocation: getLocation
}